<?php
/**
 *      [迷你云] (C)2009-2012 南京恒为网络科技.
 *   软件仅供研究与学习使用，如需商用，请访问www.miniyun.cn获得授权
 * 
 */
?>
<?php

class MEvents extends MModel {
    
    public static function CreateEvents($user_id, $user_device_id, $action, $path, $context, $event_uuid) {
        $context = str_replace("'", "\\'", $context);
        $sql = "insert into " . DB_PREFIX . "_events(user_id,action,user_device_id,";
        $sql .= "file_path,context,event_uuid,created_at,updated_at)";
        $sql .= " values (";
        $sql .= "{$user_id},";
        $sql .= "{$action},";
        $sql .= "{$user_device_id},";
        $sql .= "\"{$path}\",";
        $sql .= "'{$context}',";
        $sql .= "'{$event_uuid}',";
        $sql .= "now(),now())";
        FXP::trace ( "function: '{CreateEvents}',sql:'{$sql}'" );
        $db_manager = MDbManager::getInstance ();
        $result = $db_manager->insertDb ( $sql );
        return $result;
    }
    
    
    public static function batchCreateEvents($user_id, $user_device_id, $file_details) {
        $sql = "insert into " . DB_PREFIX . "_events(user_id,action,user_device_id,";
        $sql .= "file_path,context,event_uuid,created_at,updated_at) values ";
        foreach ( $file_details as $file_detail ) {
            $context = str_replace("'", "\\'", $file_detail->context);
            $sql .= "({$user_id},";
            $sql .= "{$file_detail->event_action},";
            $sql .= "{$user_device_id},";
            $sql .= "\"{$file_detail->from_path}\",";
            $sql .= "'{$context}',";
            $sql .= "\"{$file_detail->event_uuid}\",";
            $sql .= "now(),now()),";
        }
        
                                $sql = substr ( $sql, 0, - 1 );
        FXP::trace ( "function: '{batchCreateEvents}',sql:'{$sql}'" );
        $db_manager = MDbManager::getInstance ();
        $result = $db_manager->insertDb ( $sql );
        return $result;
    }
    
    
    public static function queryEventByEventUUID($event_uuid) {
        $db_manager = MDbManager::getInstance ();
        $sql = "select * from " . DB_PREFIX . "_events where event_uuid='{$event_uuid}'";
        FXP::trace ( "function: '{__FUNCTION__}',sql:'{$sql}'" );
        return $db_manager->selectDb ( $sql );
    }
    
    
    public static function count($condition) {
        $db_manager = MDbManager::getInstance ();
        $sql = "select count(*) as count from " . DB_PREFIX . "_events ";
        $sql .= "WHERE ";
        $sql .= $condition;
        FXP::trace ( "function: '{count}',sql:'{$sql}'" );
        $retval = $db_manager->selectDb ( $sql );
        return $retval [0] ["count"];
    }
    
    public static function findAll($condition) {
        $db_manager = MDbManager::getInstance ();
        $sql = "select * from " . DB_PREFIX . "_events ";
        $sql .= "WHERE ";
        $sql .= $condition;
        FXP::trace ( "function: '{count}',sql:'{$sql}'" );
        return $db_manager->selectDb ( $sql );
    }
}
?>